import router from "./router";
import store from "./store";
import NProgress from "nprogress"; // progress bar
import "nprogress/nprogress.css"; // progress bar style

import { MessageBox } from "element-ui";

import { get_localStorage, get_sessionStorage } from "@/utils/localToken";
import { getUserInfo } from "@/api/login"; //getAccount

NProgress.configure({
    showSpinner: false
}); // NProgress Configuration

router.beforeEach(async (to, from, next) => {
    // start progress bar
    NProgress.start();
    /* 路由发生变化修改页面title */
    if (to.meta && to.meta.title) {
        document.title = to.meta.title;
    }

    if (store.getters.userInfo) {
        if (to.path == "/") {
            next("/home");
        } else {
            next();
        }
    } else {
        var token =
            get_localStorage("k_token") || get_sessionStorage("k_token");

        if (token) {
            // 保存k_token
            store.commit("user/SETTING_K_TOKEN", token);
            var infoData = await getUserInfo({
                k_token: token
            });
            if (infoData.code == 200) {
                store.commit("user/SETTING_USERINFO", infoData.data);
                if (infoData.data.school == null) {
                    next("/perfect");
                } else {
                    next(to.fullPath);
                }
                console.log("====================================");
                console.log("路由刷新", store.state);
                console.log("====================================");
            } else {
                sessionStorage.clear();
                localStorage.clear();
                MessageBox("自动登录失败", "提示", {
                    confirmButtonText: "确定",
                    callback: () => {}
                });
                next("/");
            }
            //获取a_token
            // var userInfoData = await getAccount({
            //     k_token: token
            // });
            // if (userInfoData.code == 0) {
            //     store.commit("user/SETTING_A_TOKENINFO", userInfoData.data);
            //     //获取info
            //     var infoData = await getUserInfo({
            //         k_token: token
            //     });
            //     if (infoData.code == 200) {
            //         store.commit("user/SETTING_USERINFO", infoData.data);
            //         next(to.fullPath);
            //         console.log("====================================");
            //         console.log("路由刷新", store.state);
            //         console.log("====================================");
            //     } else {
            //         sessionStorage.clear();
            //         localStorage.clear();
            //         MessageBox("自动登录失败", "提示", {
            //             confirmButtonText: "确定",
            //             callback: () => {}
            //         });
            //         next("/");
            //     }
            // } else {
            //     sessionStorage.clear();
            //     localStorage.clear();
            //     MessageBox("自动登录失败", "提示", {
            //         confirmButtonText: "确定",
            //         callback: () => {}
            //     });
            //     next("/");
            // }
        } else {
            if (to.path != "/") {
                next("/");
            } else {
                next();
            }
        }
    }
    NProgress.done();
});

router.afterEach(() => {
    // finish progress bar
    setTimeout(() => {
        NProgress.done();
    }, 2000);
});
